﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        private void JLTAB50GB12()
        {
            try
            {
                string value = "";
                DataRow row = null;
                int x = 1, y = 1;
                x = 3; y = 5;
                string str1 = "", str2 = "", str3 = "";
                row = dsSource.Tables["ABKOPF"].Rows[0];
                value = row["ABKAUFKRS"].ToString() + "-" + row["ABKAUFLFD"].ToString();
                exAgent.SetCellValue(x, y, value);
                value = row["ABKKDAUFTNR"].ToString();
                x = 3; y = 14;
                exAgent.SetCellValue(x, y, value);
                x = 3; y = 30;
                Helper.GetDate(ref date, ref year, ref month, ref day);
                Helper.GetSplitDate(ref str1, ref str2, ref str3, date);
                exAgent.SetCellValue(3, 30, str3);
                str1 = row["ABKNEUDAT"].ToString();
                Helper.GetSplitDate(ref str1, ref str2, ref str3, str1);
                exAgent.SetCellValue(4, 30, str3); 
                int rownum = -1;
                string remark = "";
                value = "";

                int positions = 0;
                if (!dsSource.Tables.Contains("GAAUF"))
                {
                    return;
                }
                var query = from DataRow ga in dsSource.Tables["GAAUF"].AsEnumerable()
                            where ga["GASA"].ToString() == "713"
                            select ga;
                int count = query.Count();
                string art = "", var = "", color = "";
                double kg = 0, kg3 = 0;
                x = 0;
                string ndate = "";
                int rpl = 2;
                int posx = 12;
                foreach (DataRow ga in query)
                {
                    positions++;
                    art = ga["GAART"].ToString();
                    var = ga["GAVAR"].ToString();
                    color = ga["GAFARBE"].ToString();

                    kg = System.Convert.ToDouble(ga["GAKG1"].ToString()) * 0.01;
                    kg3 = System.Convert.ToDouble(ga["GAKG3"].ToString()) * 0.01;
                    if (kg3 == 0)
                    {
                        //count--;
                        //continue;
                    }
                    ndate = ga["GADATVON"].ToString();
                    Helper.GetSplitDate(ref str1, ref str2, ref str3, ndate);
                    exAgent.SetCellValue(posx, 20, str3);
                    exAgent.SetCellValue(posx, 1, Helper.GetSubRollNum(ga["GAAUFPOS"].ToString()), true);
                    exAgent.SetCellValue(posx, 24, kg, false);
                    #region=====Technical============
                    var query1 = from rows in dsSource.Tables["GSDAT"].AsEnumerable()
                                 where rows["GSART"].ToString() == art
                                 & rows["GSVAR"].ToString() == var
                                 select rows;
                    foreach (DataRow rw in query1)
                    {
                        value = GetGB2312Value(rw["GSGARNBEZ"].ToString());
                        exAgent.SetCellValue(posx, 3, art + value.Trim());
                    }
                    #endregion
                    #region=====COLOR============
                    if (dsSource.Tables.Contains("GSCOLOR"))
                    {
                        var queryc = from rows in dsSource.Tables["GSCOLOR"].AsEnumerable()
                                     where rows["GSCART"].ToString() == art
                                     & rows["GSCVAR"].ToString() == var
                                     & rows["GSCFARBE"].ToString() == color
                                     select rows;
                        foreach (DataRow rw in queryc)
                        {
                            value = GetGB2312Value(rw["GSCBEZ"].ToString());
                            exAgent.SetCellValue(posx, 16, color.Trim() + value.Trim());
                            if (color.Trim() == "000000")
                            {
                                exAgent.SetCellValue(posx, 16, value.Trim());
                            }
                        }
                    }
                    #endregion
                    #region=====composition=======
                    if (dsSource.Tables.Contains("GSMATK"))
                    {
                        var query2 = from rows in dsSource.Tables["GSMATK"].AsEnumerable()
                                     where rows["GSMART"].ToString() == art
                                     & rows["GSMVAR"].ToString() == var
                                     select rows;
                        string composition = "";
                        double tratio = 0;
                        foreach (DataRow rw in query2)
                        {
                            string sg = rw["GSMRBEZ"].ToString().Trim();
                            string bez = rw["GSMRSPZ"].ToString();
                            bez = GetGB2312Value(bez).Trim();
                            double ratio = System.Convert.ToDouble(rw["GSMRANT"].ToString()) * 0.1;
                            composition += ratio + "%" + sg;
                            tratio += ratio;
                            if (tratio >= 100)
                                break;
                        }
                        exAgent.SetCellValue(posx + 1, 5, composition);
                    }
                    #endregion
                    #region=====Alloction=======
                    if (dsSource.Tables.Contains("ZUTEI"))
                    {
                        var qzu = from rows in dsSource.Tables["ZUTEI"].AsEnumerable()
                                  where rows["ZUAUFKRS"].ToString() == ga["GAAUFKRS"].ToString()
                                     & rows["ZUAUFLFD"].ToString() == ga["GAAUFLFD"].ToString()
                                    & rows["ZUAUFPOS"].ToString() == ga["GAAUFPOS"].ToString()
                                     & rows["ZUSA"].ToString() == ga["GASA"].ToString()
                                     & rows["ZUPOS"].ToString() == ga["GAPOS"].ToString()
                                  select rows;
                        string filters = "";
                        DataRow[] rx;
                        DataRow dwPartie = null;
                        string info = "";
                        int zucount = qzu.Count();
                        int zupos = 0;
                        foreach (DataRow rw in qzu)
                        {
                            // ZUPARTIEZULO	ZULB ZUEINTKG ZUENTNKG
                            double allocated = System.Convert.ToDouble(rw["ZUEINTKG"].ToString().Trim()) * 0.001;
                            double get = System.Convert.ToDouble(rw["ZUENTNKG"].ToString().Trim()) * 0.001;
                            double TOGET = allocated - get;
                            string sppartie =rw["SELLER"].ToString().Trim() + "-" + rw["ZUSPPART"].ToString().Trim();
                            if (TOGET == 0)
                            {
                                zucount--;
                                continue;
                            }
                            exAgent.SetCellValue(posx + zupos, 27, Helper.GetGB2312Value(sppartie));
                            exAgent.SetCellValue(posx + zupos, 31, rw["GLLP"].ToString().Trim());
                            exAgent.SetCellValue(posx + zupos, 33, TOGET);
                            zupos++;
                            if (zupos > 1
                             && zupos < zucount
                             && zucount > 2)
                            {
                                exAgent.RangeCopy(13, 13, posx + zupos, posx + zupos);
                            }
                        }
                        if (zupos > 2)
                        {
                            posx = posx + zupos - 2;
                        }
                    }
                    posx = posx + rpl;
                    #endregion
                    if (positions < count)
                    {
                        exAgent.RangeCopy(12, 12 + rpl - 1, posx, posx + rpl - 1);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}